package com.example.peanutsubway.dao.impl;


import com.example.peanutsubway.dao.AdminUserDao;
import com.example.peanutsubway.entity.AdminUserinfo;
import com.example.peanutsubway.entity.RoleInfo;
import com.example.peanutsubway.util.JDBCUtils;
import com.example.peanutsubway.util.PageInfo;
import com.example.peanutsubway.vo.UserInfo;

import java.sql.Connection;
import java.util.List;

/**
 * @Author：liulei
 * @Version：1.0
 * @Date：2021/10/11-16:45
 * @Since:jdk1.8
 * @Description:
 */
public class AdminUserDaoImpl implements AdminUserDao {
    private Connection connection;

    public void setConnection(Connection connection) {
        this.connection = connection;
    }

    @Override
    public UserInfo login(String name, String pwd) {
//        String sql = "select * from pn_admin_userinfo where username = ? and pwd = ? and state=1";
        String sql = "select pn_admin_userinfo.*,pn_role.rolename from pn_admin_userinfo,pn_role where username = ? AND pwd = ? AND pn_admin_userinfo.state=1 AND pn_role.rid = pn_admin_userinfo.roleid";
        return JDBCUtils.get(sql, UserInfo.class, name, pwd);
    }

    @Override
    public List<UserInfo> findAllUser(PageInfo pageInfo) {
        String sql = "SELECT a.adminid,a.username,a.roleid,r.rolename,a.creationtime,a.lastlogintime,a.state FROM pn_admin_userinfo a,pn_role r WHERE a.roleid = r.rid and a.state=1 Limit ?,?";
        return JDBCUtils.find(sql, UserInfo.class, pageInfo.getStartIndex(), pageInfo.getPageNum());
    }

    @Override
    public List<RoleInfo> findAllRole() {
        String sql = "SELECT * FROM pn_role";
        return JDBCUtils.find(sql, RoleInfo.class);
    }

    @Override
    public Integer userCount() {
        String sql = "SELECT count(1) FROM pn_admin_userinfo Where state=1";
        return JDBCUtils.count(sql).intValue();
    }

    @Override
    public Boolean updateRoleMessage(AdminUserinfo adminUserinfo) {
        String sql = "update pn_admin_userinfo set username = ?,pwd=?,roleid=?,state=? where adminid = ?";
        return JDBCUtils.update(sql, adminUserinfo.getUsername(), adminUserinfo.getPwd(), adminUserinfo.getRoleid(), adminUserinfo.getState(), adminUserinfo.getAdminid()) > 0;
    }

    @Override
    public Boolean register(AdminUserinfo adminUserinfo) {
        String sql = "INSERT pn_admin_userinfo VALUES (null,?,?,?,?,NOW(),null)";
        if (connection!=null){
            return JDBCUtils.insert(connection,sql, adminUserinfo.getUsername(), adminUserinfo.getPwd(), adminUserinfo.getRoleid(), adminUserinfo.getState()).intValue()>0;
        }else {
            return JDBCUtils.insert(sql, adminUserinfo.getUsername(), adminUserinfo.getPwd(), adminUserinfo.getRoleid(), adminUserinfo.getState())>0;
        }
    }

    @Override
    public Boolean updateUser(AdminUserinfo adminUserinfo) {
        String sql = "UPDATE pn_admin_userinfo SET username=?,pwd=?,roleid=?,state=? WHERE adminid=?";
        return JDBCUtils.update(sql,adminUserinfo.getUsername(),adminUserinfo.getPwd(),adminUserinfo.getRoleid(),adminUserinfo.getState(),adminUserinfo.getAdminid())>0;
    }

    @Override
    public Boolean addUser(AdminUserinfo adminUserinfo) {
        String sql = "INSERT INTO pn_admin_userinfo VALUES (null,?,?,?,?,now(),null)";
        return JDBCUtils.update(sql,adminUserinfo.getUsername(),adminUserinfo.getPwd(),adminUserinfo.getRoleid(),adminUserinfo.getState())>0;
    }

    @Override
    public Boolean deleteUser(Integer id) {
        String sql = "UPDATE pn_admin_userinfo SET state=0 WHERE adminid=?";
        return JDBCUtils.update(sql,id)>0;
    }
}
